﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-9-13
Description:UserList三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class UserListService
	{
		#region 增加一条数据
		public static int AddUserList(UserList userList)
		{
			string sql =
			"INSERT INTO UserList(ParentID,link,power,paixu,master_name,touxiang) "+
			"VALUES (@ParentID,@link,@power,@paixu,@master_name,@touxiang)";
			
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
						
					new SQLiteParameter("@ParentID",userList.ParentID),
						
					new SQLiteParameter("@link",userList.Link),
						
					new SQLiteParameter("@power",userList.Power),
						
					new SQLiteParameter("@paixu",userList.Paixu),
						
					new SQLiteParameter("@master_name",userList.Master_name),
						
					new SQLiteParameter("@touxiang",userList.Touxiang)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteUserListById(int id)	
		{
			string sql="DELETE From  UserList  Where id=@id";
			 SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifyUserListByid(UserList userList)
		{
		string sql ="UPDATE "+"UserList"+" set "+
					"ParentID=@ParentID, "+
					"link=@link, "+
					"power=@power, "+
					"paixu=@paixu, "+
					"master_name=@master_name, "+
					"touxiang=@touxiang "+ 
				" Where id=@id";
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id",userList.Id),
					new SQLiteParameter("@ParentID",userList.ParentID),
					new SQLiteParameter("@link",userList.Link),
					new SQLiteParameter("@power",userList.Power),
					new SQLiteParameter("@paixu",userList.Paixu),
					new SQLiteParameter("@master_name",userList.Master_name),
					new SQLiteParameter("@touxiang",userList.Touxiang)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<UserList> GetAllUserLists()
		{
			string sqlAll = "SELECT * FROM UserList";
			return GetUserListsBySql(sqlAll);
		}
		//以标识列获得对象
		public static UserList GetUserListByid(object id)
		{
			string sql = "SELECT * FROM "+"UserList"+" WHERE id = @id";
			
            using (SQLiteDataReader reader = DBHelper.GetReader(sql, new SQLiteParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					UserList userList = new UserList();
					userList.Id=Convert.ToInt32(reader["id"]);
					userList.ParentID=Convert.ToInt32(reader["ParentID"]);
					userList.Link=Convert.ToString(reader["link"]);
					userList.Power=Convert.ToString(reader["power"]);
					userList.Paixu=Convert.ToDecimal(reader["paixu"]);
					userList.Master_name=Convert.ToString(reader["master_name"]);
					userList.Touxiang=Convert.ToString(reader["touxiang"]);
					reader.Close();
					
					return userList;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<UserList> GetUserListsBySql(string safeSql)
		{
			List<UserList> list = new List<UserList>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					UserList userList = new UserList();
					userList.Id=Convert.ToInt32(row["id"]);
					userList.ParentID=Convert.ToInt32(row["ParentID"]);
					userList.Link=Convert.ToString(row["link"]);
					userList.Power=Convert.ToString(row["power"]);
					userList.Paixu=Convert.ToDecimal(row["paixu"]);
					userList.Master_name=Convert.ToString(row["master_name"]);
					userList.Touxiang=Convert.ToString(row["touxiang"]);
					list.Add(userList);
				}
				return list;
			}
             
		}
		private static IList<UserList> GetUserListsBySql( string sql, params SQLiteParameter[] values )
		{
			List<UserList> list = new List<UserList>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					UserList userList = new UserList();
					userList.Id=Convert.ToInt32(row["id"]);
					userList.ParentID=Convert.ToInt32(row["ParentID"]);
					userList.Link=Convert.ToString(row["link"]);
					userList.Power=Convert.ToString(row["power"]);
					userList.Paixu=Convert.ToDecimal(row["paixu"]);
					userList.Master_name=Convert.ToString(row["master_name"]);
					userList.Touxiang=Convert.ToString(row["touxiang"]);
					list.Add(userList);
				}
				return list;
			}
            
			 
		}
		#endregion
		 
	}
}

